package com.poetize.plus.controller.ui;

import com.poetize.plus.entity.dto.RegisterDTO;
import com.poetize.plus.entity.dto.UserUiDTO;
import com.poetize.plus.entity.vo.UserVO;
import com.poetize.plus.service.UserService;
import com.poetize.plus.utils.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.security.PermitAll;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author zuosy
 * @created 2024/4/11 17:08
 */
@Tag(name = "用户操作")
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserUiController {

    private final UserService userService;

    @PermitAll
    @GetMapping("/sendCode/{email}")
    @Operation(summary = "获取验证码")
    public R<Boolean> sendCode(@PathVariable("email") String email){
        return R.ok(userService.sendCode(email));
    }

    @PermitAll
    @PostMapping("/register")
    @Operation(summary = "注册")
    public R<Boolean> register(@RequestBody @Validated RegisterDTO registerDTO){
        return R.ok(userService.register(registerDTO));
    }

    @GetMapping("/userInfo")
    @Operation(summary = "用户详情")
    @PreAuthorize(value = "hasAnyRole('common','administrator')")
    public R<UserVO> userInfo(){
        return R.ok(userService.userInfo());
    }

    @PostMapping("/modified")
    @Operation(summary = "修改个人信息")
    @PreAuthorize(value = "hasRole('common')")
    public R<Boolean> modified(@RequestBody @Validated UserUiDTO userUiDTO){
        return R.ok(userService.modified(userUiDTO));
    }
}
